// register.js
import util from '../../../utils/util.js';
var app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    carno: '',
    vincode: '',
    totalkm:'',
    buytime:'',
    carbrandname:'',
    cartypename:'',

    carbrandlogo:'',
    carbrandid:'',
    cartypeid:'',
    carseriesname:'',
    carseriesid:'',

    
    
      // user: app.globalData.xcx_user_info,
  },

  inputcarno: function (e) {
    this.setData({
      carno: e.detail.value
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.setNavigationBarTitle({
      title: "我的爱车"
    });

    let item = JSON.parse(options.data);

    if(item.carcode !='')
    {
      this.setData({ carno: item.carcode  });

    }
    if (item.vincode != '') {
      this.setData({ vincode: item.vincode });

    }
    if (item.totalkm != '') {
      this.setData({ totalkm: item.totalkm });

    }
 

    if (item.cartypename != '') {
      this.setData({ cartypeid: item.cartypeid });
      this.setData({ cartypename: item.cartypename });

      this.setData({ carbrandid: item.carbrandid });
      this.setData({ carbrandname: item.carbrandname });

      this.setData({ carseriesid: item.carseriesid });
      this.setData({ carseriesname: item.carseriesname });
      this.setData({ carbrandlogo: item.carbrandlogo });
    }

    
    if (item.buytime != '') {
      this.setData({ buytime: item.buytime });

    }

   
    
  },

  // 注册
  // ecapi.auth.default.signup
  formSubmit(event) {
    let self = this,
      eValue = event.detail.value;
    if (eValue.carno.length <= 0) {
      util.showToast('请输入用户名');
      return false;
    }
  

    
    var url = app.globalData.SERVER+'car/addXcxNewCar';
  
    
    // var usetinfo = wx.getStorageSync("user_info");
    var usetinfo = util.getData("userinfo");
    var tel = usetinfo.tel;
    var that = this;
    util.request(url, 'POST',{
      isAdd:'1',
      isdefault:'1',
      carbrandlogo: that.data.carbrandlogo,
      carbrandname: that.data.carbrandname,
      carbrandid: that.data.carbrandid,
      carseriesname: that.data.carseriesname,
      carseriesid: that.data.carseriesid,
      cartypename: that.data.cartypename,
      cartypeid: that.data.cartypeid,
      totalkm: eValue.totalkm,
      buytime: eValue.buytime,
      vincode: eValue.vincode,
      carcode: eValue.carno,
      tel: tel,
      }).then(res => {
      util.showToast('新增成功', 'error', 1000);
    }).catch(err => {
      if (err.data.code ===1) {
        util.showToast('数据保存成功！', 'success', 1000);
        setTimeout(function () {
          wx.navigateBack({
            delta: -1
          });
        }, 2000) //延迟时间 这里是1秒


        
      } else {
        self.setData({
          siteInfo: err.data.site_info
        });
      }

      wx.setNavigationBarTitle({
        title: self.data.siteInfo.name || util.pageTitle.home
      });
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    var pages = getCurrentPages();
    var currPage = pages[pages.length - 1];

    if (currPage.data.brand) {
      that.setData({
        carbrandname: currPage.data.brand.name,
          carbrandid: currPage.data.brand.id,
      })
    }

    if (currPage.data.series) {
      that.setData({
        carseriesname: currPage.data.series.name,
        carseriesid: currPage.data.series.id,
        carbrandlogo: currPage.data.series.logo,
       
      })
    }


    if (currPage.data.spec) {
      that.setData({
        cartypename: currPage.data.spec.name,
        cartypeid: currPage.data.spec.id,
      })
    }
   
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  bindDateChange: function (e) {
    console.log(e.detail.value)
    this.setData({
      buytime: e.detail.value
    })
  },

  bindtapFunc: function (e) {

    wx.navigateTo({
      url: 'cartype',
    });

  },
  changecar:function(e){
    wx.navigateTo({
      url: 'cartype' ,
    });
  },
});